#!/usr/bin/env node --harmony
'use strict'
const { enhanceErrorMessages, cleanArgs, getVersions, chalk } = require('./packages/cli-shared-utils/')
const program = require('commander');

program
  .version(`gvsoft-converter ${getVersions().current}`)
  .usage('<command> [options]')
program
  .command('pdf2json <pdf-file>')
  .description('convert pdf to JSON')
  .alias('p2j')
  .option('-o,--outfile <file-name>', '')
  .action((infile, cmd) => {
    require('./packages/pdf2json')(infile, cleanArgs(cmd));
  })
program.commands.forEach(c => c.on('--help', () => console.log()))

enhanceErrorMessages('missingArgument', p => `Missing required argument ${chalk.yellow(`<${p}>`)}.`)
enhanceErrorMessages('unknownOption', p => `Unknown option ${chalk.yellow(p)}.`)
enhanceErrorMessages('optionMissingArgument', (option, flag) => `Missing required argument for option ${chalk.yellow(option.flags)}` + (flag ? `, got ${chalk.yellow(flag)}` : ``))
// 未知命令会报错
program.on('command:*', function () {
  console.log('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
  process.exit(1);
})
program.parse(process.argv)
